package com.seya.calculite.app;

import android.app.Activity;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapActivity extends Activity {

    private GoogleMap googleMap;
    SharedPreferences prefs;
    double slongitude, elongitude;
    double slatitude, elatitude;
    int status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_scale);
        setContentView(R.layout.activity_map);
        FontHelper.applyFont(this, findViewById(R.id.activity_main_root), "RobotoLight.ttf");

        status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(MapActivity.this);

        prefs = this.getSharedPreferences(SplashScreenActivity.SHARED_PREFERENCES_NAME,
                MODE_PRIVATE);
        slatitude = Double.parseDouble(prefs.getString(SplashScreenActivity
                .START_LATITUDE,"0"));
        slongitude = Double.parseDouble(prefs.getString(SplashScreenActivity
                .START_LONGITUDE,"0"));

        elatitude = Double.parseDouble(prefs.getString(SplashScreenActivity
                .END_LATITUDE,"0"));
        elongitude = Double.parseDouble(prefs.getString(SplashScreenActivity
                .END_LONGITUDE,"0"));

        try {
            initilizeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initilizeMap() {
        if (googleMap == null) {
            try {
                if(status == ConnectionResult.SUCCESS) {
                    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                            R.id.map)).getMap();
                    MarkerOptions markerStart = new MarkerOptions()
                            .position(new LatLng(slatitude, slongitude))
                            .title("Start Location");
                    MarkerOptions markerEnd = new MarkerOptions()
                            .position(new LatLng(elatitude, elongitude))
                            .title("End Location");
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(
                            new LatLng(elatitude, elongitude)).zoom(13).build();

                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                    //googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// adding marker
                    googleMap.addMarker(markerStart);
                    googleMap.addMarker(markerEnd);

                    googleMap.setMyLocationEnabled(true);

                    // check if map is created successfully or not
                    if (googleMap == null) {
                        Toast.makeText(getApplicationContext(),
                                "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                                .show();
                    }
                }else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
                    Toast.makeText(getApplicationContext(),"update required",Toast.LENGTH_SHORT).show();
                    Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, MapActivity.this, 1);
                    dialog.show();
                }
                else if (status==ConnectionResult.SERVICE_MISSING){
                    Toast.makeText(getApplicationContext(),"play service not available",Toast.LENGTH_SHORT).show();
                }
            }catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(SplashScreenActivity.loggingOut){
            finish();
        }
        initilizeMap();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        //closing transition animations
        overridePendingTransition(R.anim.activity_open_scale,R.anim.activity_close_translate);
    }
}
